package com.lzx.mq.Listener;

import com.alibaba.fastjson.JSONObject;
import com.lzx.mq.feign.ActivityClient;
import com.lzx.mq.vo.VoucherOrder;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Timestamp;

@Component
@RocketMQMessageListener(consumerGroup = "secKill-group", topic = "secKill-topic")
public class SecKillListener implements RocketMQListener<String> {
    @Resource
    private ActivityClient activityClient;

    @Override
    public void onMessage(String msg) {
        JSONObject jsonObject = JSONObject.parseObject(msg);
        VoucherOrder voucherOrder = new VoucherOrder();
        voucherOrder.setId(Long.parseLong(jsonObject.get("id").toString()));
        voucherOrder.setUserId(Integer.valueOf(jsonObject.get("userId").toString()));
        voucherOrder.setVoucherId(Integer.valueOf(jsonObject.get("voucherId").toString()));
        voucherOrder.setCreateTime(new Timestamp(System.currentTimeMillis()));
        //保存订单和扣减库存
        activityClient.saveOrderAndReduceStock(voucherOrder);
    }
}
